package com.talos.ums.controller.admin.role;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.role.vo.UmsRolePageReqVO;
import com.talos.ums.controller.admin.role.vo.UmsRoleRespVO;
import com.talos.ums.controller.admin.role.vo.UmsRoleSaveReqVO;
import com.talos.ums.dal.dataobject.role.UmsRoleDO;
import com.talos.ums.service.role.UmsRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 角色表")
@RestController
@RequestMapping("/ums/role")
@Validated
public class UmsRoleController {

    @Resource
    private UmsRoleService roleService;

    @PostMapping("/create")
    @Operation(summary = "创建角色表")
    @PreAuthorize("@ss.hasPermission('ums:role:create')")
    public CommonResult<Long> createRole(@Valid @RequestBody UmsRoleSaveReqVO createReqVO) {
        return success(roleService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色表")
    @PreAuthorize("@ss.hasPermission('ums:role:update')")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody UmsRoleSaveReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:role:delete')")
    public CommonResult<Boolean> deleteRole(@RequestParam("id") Long id) {
        roleService.deleteRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:role:query')")
    public CommonResult<UmsRoleRespVO> getRole(@RequestParam("id") Long id) {
        UmsRoleDO role = roleService.getRole(id);
        return success(BeanUtils.toBean(role, UmsRoleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色表分页")
    @PreAuthorize("@ss.hasPermission('ums:role:query')")
    public CommonResult<PageResult<UmsRoleRespVO>> getRolePage(@Valid UmsRolePageReqVO pageReqVO) {
        PageResult<UmsRoleDO> pageResult = roleService.getRolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsRoleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出角色表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:role:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoleExcel(@Valid UmsRolePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsRoleDO> list = roleService.getRolePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "角色表.xls", "数据", UmsRoleRespVO.class,
                        BeanUtils.toBean(list, UmsRoleRespVO.class));
    }

}
